package user.utils;

import java.util.Scanner;

/**
 * Class makes long division of two numbers and prints result.
 *
 * @version
      1.02
 * @author
      Alex Kebkalo
 */
public class Division {
	public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int divident = inputNumber("Type divident:",scan);
        int divisor = inputNumber("Type divisor:",scan);

        scan.close();
        if ((divisor > 0) && (divident >= 0)) {
            String[] result=getDivisionResults(divident, divisor);
            printResult(result);
        } else {
            printError();
        }
	}

    /**
     *   Constants, which describe output format
     */
    private final String MINUS = "_";
    private final String UNDERLINE = "_";
    private final String DIVIDE = "|";
    private final String SPACE = " ";

    /**
     *  Methods, which implement algorythm
      */
    static String[] getDivisionResults(int divident, int divisor){
        final int BASE = 10;
        int dividentLength = getNumberLength(divident);
        int stringNumber = 0;
        int currentNumber = 0;
        int currentPosition = 0;

        String[] result=new String[getResultCount(divident, divisor)];
        for (int counter = 0; counter < result.length; counter++) {
            result[counter] = "";
        }
        while (currentPosition < dividentLength){
            while ((currentNumber < divisor) && (currentPosition < dividentLength)){
                currentNumber = currentNumber * BASE + getDigit(divident, currentPosition);
                currentPosition++;
            }
            if (currentNumber >= divisor) {
                int divNumber = currentNumber / divisor * divisor;
                result[stringNumber++] = getFormattedString(String.format("%s%d", MINUS, currentNumber),
                        currentPosition + 1);
                result[stringNumber++] = getFormattedString(divNumber, currentPosition + 1);
                result[stringNumber++] = getFormattedString(fillString(UNDERLINE, getNumberLength(currentNumber)),
                        currentPosition + 1);
                currentNumber -= divNumber;
            }
        }
        result[stringNumber] = getFormattedString(currentNumber, currentPosition + 1);
        result[0] = String.format("%s%d %s%d", result[0].substring(0, 1), divident, DIVIDE, divisor);
        String spaces=getFormattedString("", result[0].length() - result[1].length() - getNumberLength(divisor));
        result[1]=String.format("%s%s%s", result[1], spaces, fillString(UNDERLINE, getNumberLength(divident / divisor)));
        result[2]=String.format("%s%s%d", result[2], spaces, divident / divisor);
        return result;
    }

    static int getResultCount(int divident, int divisor){
        final int BASE_STRING_COUNT = 3;

        if (divident < divisor) {
            return BASE_STRING_COUNT;
        } else {
            return BASE_STRING_COUNT * getNumberLength(divident / divisor) + 1;
        }
    }

    /**
     *  String utilities
      */
	static String fillString(String filler, int number) {
		String resultString = "";

		for (int i = 0; i < number; i++) {
			resultString += filler;
        }
		return resultString;
	}

    static String getFormattedString(String value, int length){
        String resultString = String.format("%s%s", fillString(SPACE, length-value.length()), value);

        return resultString;
    }

    static String getFormattedString(int value, int length){
        String resultString = getFormattedString(String.valueOf(value), length);

        return resultString;
    }

    /**
     *  Number utilities
      */
    static int getDigit(int number, int position) {
        return Integer.parseInt(String.valueOf(number).substring(position, position + 1));
    }

	static int getNumberLength(int number) {
		return String.valueOf(number).length();
	}

    /**
     *  Input-Output
     */
    static int inputNumber(String inputmessage, Scanner scan){
        int number;

        System.out.print(inputmessage);
        number = scan.nextInt();
        return number;
    }

    static void printResult(String[] result){
        System.out.println();
        for (int counter = 0; counter < result.length; counter++) {
            if (result[counter] != "") {
                System.out.println(result[counter]);
            }
        }
    }

    static void printError(){
        System.out.println();
        System.out.println("Wrong input data.");
    }
}
